package ch.rheintalconnection.sysventory.presentation.panels;

import java.sql.Date;
import java.util.Calendar;

import ch.rheintalconnection.sysventory.businessServices.DataService;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.VerticalLayout;

public class StatisticScansInTimePanel extends VerticalLayout implements Property.ValueChangeListener{

	private static final long serialVersionUID = -6245651047121265568L;

	private ListSelect listSelect;
	private Label infoLabel;
	private DataService dataService;
	
	public StatisticScansInTimePanel(DataService dataService){
		this.dataService = dataService;
		listSelect = new ListSelect("Anzahl der durchgeführten Scans diese/diesen/dieses");
		listSelect.setImmediate(true);
		listSelect.setNullSelectionAllowed(false);
		listSelect.addListener(this);
		this.addComponent(listSelect);
		listSelect.addItem(SelectionEnum.Week);
		listSelect.addItem(SelectionEnum.Month);
		listSelect.addItem(SelectionEnum.Year);
		
		infoLabel = new Label("");
		this.addComponent(infoLabel);
		
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		
		SelectionEnum selection = (SelectionEnum)listSelect.getValue();
		
		Calendar aktDate = Calendar.getInstance();
		
		if(selection == SelectionEnum.Week){
			
			aktDate.add(Calendar.DAY_OF_MONTH, -7);
			Date date = new Date(aktDate.get(Calendar.MILLISECOND));
			int anz = dataService.getScanQty(date);
			
			infoLabel.setValue("Scans letzte sieben Tage: " + anz);
		}else if (selection == SelectionEnum.Month) {
			aktDate.add(Calendar.MONTH, -1);
			Date date = new Date(aktDate.get(Calendar.MILLISECOND));
			int anz = dataService.getScanQty(date);
			infoLabel.setValue("Scans letzten 30/31 Tage: " + anz);
		}else if (selection == SelectionEnum.Year) {
			aktDate.add(Calendar.YEAR, -1);
			Date date = new Date(aktDate.get(Calendar.MILLISECOND));
			int anz = dataService.getScanQty(date);
			infoLabel.setValue("Scans letzte 365 Tage: " + anz);
		}
		
	}

	private enum SelectionEnum {
		Week,
		Month,
		Year;
		
		private String getText(){
			String sText = "";
			
			switch (this) {
			case Week:
				sText = "Woche";
				break;
			case Month:
				sText = "Monat";
				break;
			case Year:
				sText = "Jahr";
				break;

			default:
				throw new IllegalArgumentException();
			}
			return sText;
		}
		
		@Override
		public String toString(){
			return this.getText();
		}
		
	}

}
